﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;
using Camping_Project.Forms;
using System.Deployment.Application;

namespace Camping_Project
{
    public partial class FormsAcceuil : Form
    {
        public FormsAcceuil()
        {
            InitializeComponent();
        }

        private void FormsAcceuil_Load(object sender, EventArgs e)
        {

            this.tfLogin.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);
            this.tfMdp.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);

        }

        //Bouton de connection 
        private void Connection_Click(object sender, EventArgs e)
        {
            authentification();
        }


        public void authentification()
        {
            Boolean status = false;
            int IDlogin = 0;
            string nomPrenom = "";

            using (Camping_BDD bdd = new Camping_BDD())
            {

                var query = from emp in bdd.Employe
                            select new { emp.Login, emp.Mdp, emp.Nom, emp.Prenom,emp.IDEmploye };

                //Boucle de tous les éléments sélectionnés dans la BDD
                foreach (var e in query)
                {
                    //MessageBox.Show(e.Login + " " + e.Mdp + " | " + tfLogin + " " + tfMdp);
                    if (e.Login.Equals(tfLogin.Text) && e.Mdp.Equals(tfMdp.Text))
                    {
                        status = true;
                        IDlogin = e.IDEmploye;
                        nomPrenom = e.Prenom.Trim() + " " + e.Nom.Trim();
                        break;
                    }
                    else
                    {
                        status = false;
                    }
                }

                //Verification du status de connection 
                if (status == true)
                {

                    MessageBox.Show("Connection réussie", "Succes", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Hide();
                    //Création de la nouvelle winform
                    FormEmploye formEmploye = new FormEmploye(IDlogin,nomPrenom);
                    formEmploye.ShowDialog();
                    this.Close();

                }
                else
                {
                    MessageBox.Show("Mot de passe/Login invalide", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }

            }
        }

        private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                authentification();
            }
        }

    }
}
